import axios from "axios";
import {message} from "antd";
import jsCookie from "js-cookie";


axios.defaults.withCredentials=true;


axios.interceptors.response.use((config) => {
    return Promise.resolve(config.data)
}, (error) => {
    const result =error?.response?.data
    if(error.response?.status === 401){
        jsCookie.remove("Authorization");
        jsCookie.remove("info");
        message.error(result.error)
        window.location.href="/ouyu/login"
    }else if (result?.error){
        message.error(result.error)
    }else{
        message.error("网络错误！")
    }

    return Promise.reject(error);
})

axios.interceptors.request.use((config)=>{
    const info = JSON.parse(jsCookie.get("info") || "{}")
    if (config.method === "get"){
        config.params={
            companyId:info.company_id,
            ...(config.params||{})
        }
    }else{
        config.data.companyId = config.data.companyId || info.company_id
    }
    return config;
})
export default axios
